package com.dunjx.rabbitMq.simple;

import com.rabbitmq.client.*;
import lombok.SneakyThrows;
import org.testng.annotations.Test;


import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

public class conntction {


    public static Connection getConnection() throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");
        connectionFactory.setHost("39.103.219.84");
        connectionFactory.setVirtualHost("/");
        return connectionFactory.newConnection("测试链接");
    }




    public static void provider01(Integer i) throws Exception {

        Connection connection = null;
        Channel channel = null;
        try {
            connection = getConnection();
            String channelName = "queue05";
            String message = "direct 绑定模式  只有 5 6  能 收到"+i;
            String routingKey = "com.order.course.user.*";
            String exchangeType = "direct";
            String exchangeName="direct_message_exchange";
            channel = connection.createChannel();

            // 交换机声明
            channel.exchangeDeclare(exchangeName,exchangeType,true);
            // 队列声明
            channel.queueDeclare("queue05",true,false,false,null);
            channel.queueDeclare("queue06",true,false,false,null);
            channel.queueDeclare("queue07",true,false,false,null);
            // 绑定队列和交换机
            channel.queueBind("queue05",exchangeName,"order");
            channel.queueBind("queue06",exchangeName,"order");
            channel.queueBind("queue07",exchangeName,"course");


            channel.basicPublish(exchangeName, "order", null, message.getBytes(StandardCharsets.UTF_8));
            System.out.println("消息发送成功");
        } catch (Exception e) {
            System.out.println("发送消息发生异常");
            e.printStackTrace();
        }

        if (channel != null && channel.isOpen()) {
            channel.close();
        }
        if (connection != null && connection.isOpen()) {
            connection.close();
        }
    }


    public static Runnable runnable = new Runnable() {
        @SneakyThrows
        @Override
        public void run() {
            Channel channel = null;
            Connection connection = getConnection();
            // 获取队列的名称
            try {
                String queueName = Thread.currentThread().getName();
                channel = connection.createChannel();
                channel.basicConsume(queueName, true, new DeliverCallback() {
                    @Override
                    public void handle(String consumerTag, Delivery message) throws IOException {
                        System.out.println(queueName+"接收的 = "+ new String(message.getBody(), "UTF-8"));
                    }
                }, new CancelCallback() {
                    @Override
                    public void handle(String consumerTag) throws IOException {
                    }
                });
                System.out.println(queueName+":开始接受消息");
                System.in.read();
            } catch (IOException e) {
                System.out.println("接受消息异常");
                e.printStackTrace();
            } finally {
                if (channel != null && channel.isOpen()) {
                    channel.close();
                    System.out.println("通道关闭");
                }
                if (connection != null && connection.isOpen()) {
                    connection.close();
                    System.out.println("链接关闭");

                }

            }
        }
    };

    public static void main(String[] args) throws Exception {
        new Thread(runnable,"queue01").start();
        new Thread(runnable,"queue02").start();
        new Thread(runnable,"queue03").start();
        new Thread(runnable,"queue04").start();
        new Thread(runnable,"queue05").start();
        new Thread(runnable,"queue06").start();
    }
    @Test
    public void createMessage () throws Exception {
        for (int i = 1; i <= 2; i++) {
            provider01(i);
        }
    }
}
